package com.kxq.zhuoyue.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxq.zhuoyue.mapper.EmpMapper;
import com.kxq.zhuoyue.pageUtilEntity.EmpDept;
import com.kxq.zhuoyue.po.Clients;
import com.kxq.zhuoyue.po.Emp;
import com.kxq.zhuoyue.service.EmpService;
import com.kxq.zhuoyue.util.WeChatUtil;
import com.kxq.zhuoyue.util.wechat.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    public List<Emp> findEmpName(){
        List<Emp> empName = empMapper.findEmpName();
        return empName;
    }

    //获取客户统计
    public List<Emp> getUserList(String EmpId) {
        //从对象中提取凭证
        String corpsecret="KGmpwdvAm2m_XgIDYsKtd8ViBGHDZ2QRXysG77fVydA";
        AccessToken accessToken = WeChatUtil.getAccessToken(corpsecret);
        String ACCESS_TOKEN = accessToken.getAccess_token();
        //拼接企业微信接口调用地址  拼接对应参数
        // String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=" + ACCESS_TOKEN;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_user_behavior_data?access_token=" + ACCESS_TOKEN+"&userid="+EmpId;

        String result = "";
        List<Emp> chatList=new ArrayList<>();
        try {
            URL postUrl = new URL(url);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();

            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            result = new String(jsonBytes, "UTF-8");
            Map<String,Object> map = JSON.parseObject(result, Map.class);
            List<Map<String, String>> chat_list = (List<Map<String, String>>) map.get("clients");

            /*for (Map<String, String> item : chat_list) {
                chatList.add(getGroupMessage(item.get("chat_id"),companyId));
            }*/


        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return chatList;
    }

    //获取外部好友
    public List<Emp> outside(String EmpId) {
        //从对象中提取凭证
        String corpsecret="KGmpwdvAm2m_XgIDYsKtd8ViBGHDZ2QRXysG77fVydA";
        AccessToken accessToken = WeChatUtil.getAccessToken(corpsecret);
        String ACCESS_TOKEN = accessToken.getAccess_token();
        //拼接企业微信接口调用地址  拼接对应参数
        // String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=" + ACCESS_TOKEN;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list?access_token=" + ACCESS_TOKEN+"&userid="+EmpId;

        String result = "";
        List<Emp> chatList=new ArrayList<>();
        try {
            URL postUrl = new URL(url);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();

            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            result = new String(jsonBytes, "UTF-8");
            Map<String,Object> map = JSON.parseObject(result, Map.class);
            List<Map<String, String>> chat_list = (List<Map<String, String>>) map.get("clients");

            /*for (Map<String, String> item : chat_list) {
                chatList.add(getGroupMessage(item.get("chat_id"),companyId));
            }*/


        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return chatList;
    }

    //获取内部好友
    public List<Emp> inside(String deptId) {
        //从对象中提取凭证
        String corpsecret="KGmpwdvAm2m_XgIDYsKtd8ViBGHDZ2QRXysG77fVydA";
        AccessToken accessToken = WeChatUtil.getAccessToken(corpsecret);
        String ACCESS_TOKEN = accessToken.getAccess_token();
        //拼接企业微信接口调用地址  拼接对应参数
        // String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=" + ACCESS_TOKEN;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" + ACCESS_TOKEN+"&department_id="+deptId;

        String result = "";
        List<Emp> chatList=new ArrayList<>();
        try {
            URL postUrl = new URL(url);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();

            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            result = new String(jsonBytes, "UTF-8");
            Map<String,Object> map = JSON.parseObject(result, Map.class);
            List<Map<String, String>> chat_list = (List<Map<String, String>>) map.get("clients");

            /*for (Map<String, String> item : chat_list) {
                chatList.add(getGroupMessage(item.get("chat_id"),companyId));
            }*/


        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return chatList;
    }

    //联系客户统计
    public List<Emp> statistics() {
        //从对象中提取凭证
        String corpsecret="KGmpwdvAm2m_XgIDYsKtd8ViBGHDZ2QRXysG77fVydA";
        AccessToken accessToken = WeChatUtil.getAccessToken(corpsecret);
        String ACCESS_TOKEN = accessToken.getAccess_token();
        //拼接企业微信接口调用地址  拼接对应参数
        // String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=" + ACCESS_TOKEN;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_user_behavior_data?access_token=" + ACCESS_TOKEN;

        String result = "";
        List<Emp> chatList=new ArrayList<>();
        try {
            URL postUrl = new URL(url);
            HttpURLConnection http = (HttpURLConnection) postUrl.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            http.setDoOutput(true);
            http.setDoInput(true);
            // 连接超时30秒
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
            // 读取超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000");
            http.connect();

            InputStream inputStream = http.getInputStream();
            int size = inputStream.available();
            byte[] jsonBytes = new byte[size];
            inputStream.read(jsonBytes);
            result = new String(jsonBytes, "UTF-8");
            Map<String,Object> map = JSON.parseObject(result, Map.class);
            List<Map<String, String>> chat_list = (List<Map<String, String>>) map.get("clients");

            /*for (Map<String, String> item : chat_list) {
                chatList.add(getGroupMessage(item.get("chat_id"),companyId));
            }*/


        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return chatList;
    }


    @Override
    public Map getJop() {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        List<Emp> list = empMapper.selectList(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        list.stream().map((item) -> {
            if (!map.containsKey(item.getEmpJob())){
                map.put(item.getEmpJob(),item);
            }
            return null;
        }).collect(Collectors.toList());
        return map;
    }

    @Override
    public EmpDept getEmpById(String empId) {
        return empMapper.getEmpById(empId);
    }
}
